//
//  MLWTipsICookVideoViewController.m
//  KITCHEN_STORIES
//
//  Created by 孟令通 on 16/6/8.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWTipsViewController.h"

#import "MLWTipsScrollModel.h"

#import "MLWTipsICookVideoModel.h"

#import <SDCycleScrollView.h>

#import "MLWTipsVideoCell.h"

#import "MLWWebView.h"


#import "MLWTipsICookVideoViewController.h"

#import "MLWTipsICookVideoDetailViewController.h"

@interface MLWTipsICookVideoViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, SDCycleScrollViewDelegate>

/** 滚动视图数据源数组 */
@property (nonatomic, retain) NSMutableArray *scrollDataSourceArray;

/** 滚动视图model数组 */
@property (nonatomic, retain) NSMutableArray *scrollModelSourceArray;

/** 主集合视图的数据源数组 */
@property (nonatomic, retain) NSMutableArray *mainDataSourceArray;

/** 集合视图 */
@property (nonatomic, retain) UICollectionView *mainCollectionView;

@property (nonatomic, retain) SDCycleScrollView *scrollView;

@property (nonatomic, retain) MBProgressHUD *waitingHUD;

@end

@implementation MLWTipsICookVideoViewController

- (void)dealloc
{
    [_scrollDataSourceArray release];
    
    _mainCollectionView.delegate = nil;
    _mainCollectionView.dataSource = nil;
    [_mainCollectionView release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 添加加载图标
    self.waitingHUD = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    
    self.waitingHUD.mode = MBProgressHUDModeIndeterminate;
    
    self.waitingHUD.labelText = @"玩命加载中...";
    
    self.waitingHUD.progress = 0.3f;
    
    self.waitingHUD.animationType = MBProgressHUDAnimationZoom;
    
    self.scrollDataSourceArray = [NSMutableArray array];
    
    self.mainDataSourceArray = [NSMutableArray array];
    
    self.scrollModelSourceArray = [NSMutableArray array];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"VIDEO";
    
    [self askForScrollDataWithUrl:SCROLL_IMAGE];
    
    [self askForMainCollectionDataWithUrl:ICOOKING_URL Body:ICOOKING_VIDEO_BODY(@"1")];
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

-(void)loadView
{
    [super loadView];
    
    [self createMainCollectionView];
    
}

#pragma mark 创建集合视图
- (void) createMainCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    CGFloat scale =  357.0 / 706.0;
    
    flowLayout.itemSize = CGSizeMake(ScreenWidth - 44, (ScreenWidth - 44) * scale);
    
    flowLayout.minimumLineSpacing = 20;
    
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64 - 49) collectionViewLayout:flowLayout];
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.mainCollectionView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:_mainCollectionView];
    [_mainCollectionView release];
    
    // 注册cell
    [self.mainCollectionView registerClass:[MLWTipsVideoCell class] forCellWithReuseIdentifier:@"tipsMainCell"];
    
    // 注册头视图
    [self.mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"tipsScroll"];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

#pragma mark 请求 滚动视图 数据
- (void) askForScrollDataWithUrl:(NSString *)url
{
    
    [MLWNetTool GetNetWithUrl:url body:nil header:nil response:MLWJSON success:^(id result) {
        
        NSArray *arr = [[(NSDictionary *)result objectForKey:@"data"] objectForKey:@"bannerList"];
        
        [LTArchiver archiverObject:arr ByKey:@"scrollImage" WithPath:@"scrollImage.plist"];
        
        
        [self analysisForScrollDataWithArray:arr];
        
        
    } failure:^(NSError *error) {
        
        NSArray *arr = [LTArchiver unarchiverObjectByKey:@"scrollImage" WithPath:@"scrollImage.plist"];
        
        [self analysisForScrollDataWithArray:arr];
        
    }];
    
    
}

#pragma mark 解析 滚动视图 数据
- (void) analysisForScrollDataWithArray:(NSArray *)arr
{
    
    for (NSDictionary *dic in arr) {
        
        MLWTipsScrollModel *model = [[MLWTipsScrollModel alloc] initWithDic:dic];
        
        NSString *imageSrc = IMAGE_URL(model.image);
        
        [self.scrollDataSourceArray addObject:imageSrc];
        
        [self.scrollModelSourceArray addObject:model];
        [model release];
        
    }
    
    [self.mainCollectionView reloadData];
    
}

#pragma mark 请求主集合视图数据
- (void) askForMainCollectionDataWithUrl:(NSString *)url Body:(NSString *)body
{
    
    [MLWNetTool PostNetWithUrl:url body:body bodyStyle:MLWBodyString header:nil response:MLWJSON success:^(id result) {
        
        // 加载图标消失
        [self.waitingHUD hide:YES afterDelay:0.3];
        
        NSArray *arr = [[(NSDictionary *)result objectForKey:@"data"] objectForKey:@"data"];
        
        [LTArchiver archiverObject:arr ByKey:@"ICookMainData" WithPath:@"ICookMainData.plist"];
        
        [self analysisForMainCollectionDataWithArray:arr];
        
        
        
    } failure:^(NSError *error) {
        
        NSArray *arr = [LTArchiver unarchiverObjectByKey:@"ICookMainData" WithPath:@"ICookMainData.plist"];
        
        [self analysisForMainCollectionDataWithArray:arr];
        
    }];
    
    
}


#pragma mark 解析 主集合视图数据
- (void) analysisForMainCollectionDataWithArray:(NSArray *)arr
{
    for (NSDictionary *dic in arr) {
        
        MLWTipsICookVideoModel *model = [[MLWTipsICookVideoModel alloc] initWithDic:dic];
        
        [self.mainDataSourceArray addObject:model];
        [model release];
        
    }
    
    [self.mainCollectionView reloadData];
}

#pragma mark -
#pragma mark <SDCycleScrollViewDelegate>
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    MLWWebView *web = [[MLWWebView alloc] init];
    
    MLWTipsScrollModel *model = [self.scrollModelSourceArray objectAtIndex:index];
    
    web.url = model.url;
    
    [self.navigationController pushViewController:web animated:YES];
    
}

#pragma mark -
#pragma mark 继承自scrollview 的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    //    CGFloat width = ScreenWidth;
    //    CGFloat yOffset = scrollView.contentOffset.y;
    //
    //    if (yOffset < 0) {
    //
    //        CGFloat totalOffset = 200 + ABS(yOffset);
    //        CGFloat f = totalOffset / 200;
    //
    //        self.scrollView.frame = CGRectMake(- (width * f - width) / 2, yOffset, width * f, totalOffset);
    //
    //    }
    
}


#pragma mark -
#pragma mark <UICollectionViewDelegate, UICollectionViewDataSource>
#pragma mark 返回item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mainDataSourceArray.count;
}

#pragma mark cell重用
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MLWTipsVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tipsMainCell" forIndexPath:indexPath];
    
    MLWTipsICookVideoModel *model = [self.mainDataSourceArray objectAtIndex:indexPath.item];
    
    cell.model = model;
    
    return cell;
}

#pragma mark cell 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MLWTipsICookVideoModel *model = [self.mainDataSourceArray objectAtIndex:indexPath.item];
    
    MLWTipsICookVideoDetailViewController *cookVC = [[MLWTipsICookVideoDetailViewController alloc] init];
    
    cookVC.cateId = model.dishes_id;
    
    [self.navigationController pushViewController:cookVC animated:YES];
}

#pragma mark 为section设置头视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"tipsScroll" forIndexPath:indexPath];
        
        CGFloat scale = 354.0 / 630.0;
        
        self.scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth * scale) delegate:self placeholderImage:[UIImage imageNamed:@"TipsWhite"]];
        
        self.scrollView.imageURLStringsGroup = self.scrollDataSourceArray;
        
        self.scrollView.autoScroll = YES;
        
        self.scrollView.showPageControl = YES;
        
        self.scrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        
        [headerView addSubview:_scrollView];
        [_scrollView release];
        
        return headerView;
    }
    
    return nil;
    
}

#pragma mark 设置section头视图的高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    CGFloat scale = 354.0 / 630.0;
    
    return CGSizeMake(0, ScreenWidth * scale + 20);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
